package com.Jano.service.impl;

import com.Jano.entity.Reply;
import com.Jano.exception.BusinessException;
import com.Jano.mapper.ReplyMapper;
import com.Jano.service.ReplyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * description: ReplyServiceImpl <br>
 * date: 2022/7/12 8:46 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@Service
public class ReplyServiceImpl extends AbstractBaseServiceImpl implements ReplyService {

    @Autowired
    ReplyMapper replyMapper;

    @Override
    public List<Reply> findReplyByInvitationId(Integer invitationId) {
        throwIfNull(invitationId,"参数不能为空");
        List<Reply> replies = replyMapper.selectReplyByInvitationId(invitationId);
        return replies;
    }

    @Override
    public Boolean addReply(Integer invitationId, String content, String author) {
        if (StringUtils.isBlank(content) || Objects.isNull(content)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "提交内容不可以为空");
        }
        if (StringUtils.isBlank(author) || Objects.isNull(author)) {
            author = "匿名用户";
        }
        Date createDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(createDate);
        try {
            createDate = simpleDateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            Boolean success = replyMapper.InsertReply(invitationId, content, author, createDate);
            return success;
        }
    }
}
